package cn.facetoken.homer;

import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import cn.openbiotoken.android.facetoken.HomoSapiensFaceBitmapBiometricsFactory;

public class FaceCameraActivity extends AppCompatActivity {

    private Camera camera;
    private int cameraId;

    private ViewGroup previewContainer;
    private ActivityCameraPreview preview;
    private ImageView matchedPhoto;
    private TextView matchedName;

    private ImageView cameraShotView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_camera);

        Toolbar toolbar = findViewById(R.id.demonstration_toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show());

        // Show the Up button in the action bar.
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        cameraShotView = findViewById(R.id.camera_shot);

        // Create an instance of Camera
        camera = getCamera(this);
        Log.d(getClass().getSimpleName(), "onCreateView(): Succeeded to get the front Camera.");

        previewContainer = findViewById(R.id.camera_preview_container);
        Log.d(getClass().getSimpleName(), "onCreateView(): Succeeded to get a Frame Layout to contain a preview.");
        previewContainer.removeAllViews();

        @NonNull final TextView faceCountText = findViewById(R.id.field_face_count);

        // Create our Preview view and set it as the content of our activity.
        preview = new ActivityCameraPreview(this, camera, new HomoSapiensFaceBitmapBiometricsFactory(this), cameraShotView, faceCountText);
        //preview.setBackgroundColor(getResources().getColor(R.color.design_default_color_surface, null));
        Log.d(getClass().getSimpleName(), "onCreateView(): Succeeded to create a Camera Preview.");

        previewContainer.addView(preview);
        Log.d(getClass().getSimpleName(), "onCreateView(): Succeeded to add a preview into the preview container.");

        matchedPhoto = findViewById(R.id.field_matched_photo);
        matchedName = findViewById(R.id.field_matched_name);
    }

    public void updateTopSimilarSample(@NonNull final FaceSample faceSample) {
        //previewContainer.removeAllViews();
        //previewContainer.setVisibility(View.GONE);
        //preview.setVisibility(View.GONE);

        //matchedPhoto.setImageURI(Uri.parse(faceSample.sampleUri));
        //matchedPhoto.setVisibility(View.VISIBLE);

        matchedName.setText(faceSample.name);
        matchedName.setVisibility(View.VISIBLE);
    }

    /** A safe way to get an instance of the Camera object. */
    @Nullable
    public static Camera getCamera(final Context context){
        Camera camera = null;
        try {
            final int count = Camera.getNumberOfCameras();
            Log.i(context.getClass().getSimpleName(), String.format("getCamera(): Number of cameras is %d.", count));
            @NonNull Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            for (int i = 0; i < count; i++) {
                Camera.getCameraInfo(i, cameraInfo);
                Log.i(context.getClass().getSimpleName(), String.format("getCamera(): cameraInfo(%d): facing = %d, orientation = %d.", i, cameraInfo.facing, cameraInfo.orientation));
                if (Camera.CameraInfo.CAMERA_FACING_FRONT==cameraInfo.facing) {
                    camera = Camera.open(i);
                }
            }
        }
        catch (final Exception ignored){
            // Camera is not available (in use or does not exist)
            Log.e(context.getClass().getSimpleName(), "getCamera(): Failed to open front camera.", ignored);
        }

        if (null==camera) {
            Log.e(context.getClass().getSimpleName(), "getCamera(): Camera is not opened.");
        }
        return camera; // returns null if camera is unavailable
    }

    public int getCameraId() {
        return cameraId;
    }
}
